home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / tkern10.zip / SRC\PROCESS.C < prev    next >
Text File  |  1994-07-10  |  19KB  |  921 lines

  1. /*
  2.  *  This file forms part of "TKERN" - "Troy's Kernel for Windows".
  3.  *
  4.  *  Copyright (C) 1994  Troy Rollo <troy@cbme.unsw.EDU.AU>
  5.  *
  6.  *  This library is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU Library General Public
  8.  *  License as published by the Free Software Foundation; either
  9.  *  version 2 of the License, or (at your option) any later version.
  10.  *
  11.  *  This library is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  *  Library General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU Library General Public
  17.  *  License along with this library; if not, write to the Free
  18.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /*
  22.  * This module handles task and process management.
  23.  *
  24.  * Tasks are Windows objects, identified by a task handle. Task handles
  25.  * can be reused without us waiting for them, so we can't use them to
  26.  * track parent-child relationships or to impliment the wait calls.
  27.  *
  28.  * Processes are tkern only objects which overcome these limitations.
  29.  * Every task is assigned a process number when it enters the system.
  30.  * When it leaves the system, if its parent is a tkern process, and
  31.  * it was created from tkern_exec, a zombie process is created, for which
  32.  * the parent must wait.
  33.  */
  34.  
  35. #include <stdio.h>
  36.  
  37. #include <windows.h>
  38. #include <toolhelp.h>
  39. #include <stdlib.h>
  40. #include <memory.h>
  41. #include <string.h>
  42. #include <alloc.h>
  43. #include <stdarg.h>
  44. #include <errno.h>
  45. #include <sys/tfile.h>
  46. #include <sys/task.h>
  47. #include <sys/ioctl.h>
  48. #include <sys/wait.h>
  49. #include <sys/tkern.h>
  50.  
  51. extern    struct    tfile    _files[];
  52.  
  53. struct    tk_process _process[N_TKPROCS];
  54. struct    tk_process _zombies[N_TKPROCS];
  55. struct    task    _tasks[TNTASK];
  56.  
  57. int    nTasks = 0;    // Doesn't count fledgelings
  58.  
  59. static    int    nProcesses = 0;
  60. static    int    nZombies = 0;
  61. static    struct    task    *ptNext = 0;
  62. static    struct    task    *ptLast = 0;
  63. static    struct    task    *ptParent = 0;
  64. static    HTASK        htaskParent = 0;
  65. static    short        pidParent = 0;
  66. static    short        pidChild = 0;
  67. static    short    pidCurrent;
  68. static    short    nPIDNext = 3;    // The first PID is 2, but that's fixed up
  69.                 // in WinMain.
  70.  
  71. static    void    task_is_dead(    int    iTask);
  72.  
  73. struct task *
  74. GetTaskInfo(void)
  75. {
  76.     HTASK    hTask;
  77.     int    i;
  78.  
  79.     hTask = GetCurrentTask();
  80.     for (i = 0; i < TNTASK; i++)
  81.     {
  82.         if (_tasks[i].hTask == hTask)
  83.         {
  84.             /* If the task is in fledgeling mode,
  85.              * don't deliver signals.
  86.              */
  87.             if (_tasks[i].nSignal &&
  88.                 _tasks[i].iFledgeling == -1)
  89.             {
  90.                 (*_tasks[i].intrproc)(_tasks[i].nSignal);
  91.                 _tasks[i].nSignal = 0;
  92.             }
  93.  
  94.  
  95.             while (_tasks[i].iFledgeling != -1)
  96.                 i = _tasks[i].iFledgeling;
  97.             return &_tasks[i];
  98.         }
  99.     }
  100.     if (ptNext)
  101.     {
  102.         /* This should be impossible in newer apps.
  103.          * Older ones may get screwed up by Borland's
  104.          * _setupio routine, which calls isatty, invoking
  105.          * tkern_isatty and causing premature registration.
  106.          *
  107.          * We know this is safe because if we get here, the
  108.          * task has not registered yet, and consequently can
  109.          * not have yielded. We also know that we must be
  110.          * a TKERN app to cause GetTaskInfo() to be called.
  111.          */
  112.         return ptNext;
  113.     }
  114.     for (i = 0; i < TNTASK; i++)
  115.     {
  116.         if (!_tasks[i].hTask)
  117.         {
  118.             _tasks[i].hTask = hTask;
  119.             _tasks[i].pchCreatedBy = *((char **) ((char *) &hTask + 6));
  120.             return &_tasks[i];
  121.         }
  122.     }
  123.     return 0;
  124. }
  125.  
  126. void
  127. inc_pid(short *pid)
  128. {
  129.     if (*pid == 32767)
  130.         *pid = 2;
  131.     else
  132.         (*pid)++;
  133. }
  134.  
  135.  
  136. /*
  137.  * aiExecErrors translates between WinExec errors and errno errors
  138.  */
  139.  
  140. static    int    aiExecErrors[] =
  141. {
  142.     ENOMEM,
  143.     EFAULT,
  144.     ENOENT,
  145.     ENOENT,
  146.     EFAULT,
  147.     ENOEXEC,
  148.     ENOEXEC,
  149.     EFAULT,
  150.     EFAULT,
  151.     EFAULT,
  152.     ENOEXEC,
  153.     ENOEXEC,
  154.     ENOEXEC,
  155.     ENOEXEC,
  156.     ENOEXEC,
  157.     ENOEXEC,
  158.     ETXTBSY,
  159.     ETXTBSY,
  160.     ENOEXEC,
  161.     EFAULT,
  162.     EFAULT,
  163.     EFAULT,
  164.     EFAULT,
  165.     EFAULT,
  166.     EFAULT,
  167.     EFAULT,
  168.     EFAULT,
  169.     EFAULT,
  170.     EFAULT,
  171.     EFAULT,
  172.     EFAULT,
  173.     EFAULT,
  174.     EFAULT
  175. };
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. /* A normal POSIX exec would require pchPath, vaArgs and vaEnv.
  183.  * TKERN's also takes "pchCmdLine", the original command line.
  184.  * this is used by programs that don't use TKERN, and is the
  185.  * value used for WinExec, with pchPath prepended. If the program
  186.  * uses TKERN, it will call in to register, and then we tell it
  187.  * about its real arguments and environment.
  188.  *
  189.  * Yes, we inherit environment too.
  190.  */
  191.  
  192. short far _export
  193. tkern_exec(    char const    *pchPath,
  194.         va_list        vaArgs,
  195.         va_list        vaEnv,
  196.         char const    *pchCmdLine)
  197. {
  198.     struct    task *pt;
  199.     char    *pchCommand;
  200.     int    iNew;
  201.  
  202.     pt = GetTaskInfo();
  203.     if (pt->nFlags & TF_EXEC)    /* Attempt to recursively exec */
  204.     {
  205.         pt->nError = EAGAIN;
  206.         return -1;
  207.     }
  208.     pt->nFlags |= TF_EXEC;
  209.     Copy_Array(&pt->argv, (char **) vaArgs);
  210.     Copy_Array(&pt->envp, (char **) vaEnv);
  211.     pchCommand = (char *) malloc(strlen(pchPath) + strlen(pchCmdLine) + 2);
  212.     strcpy(pchCommand, pchPath);
  213.     if (pchCmdLine)
  214.     {
  215.         strcat(pchCommand, " ");
  216.         strcat(pchCommand, pchCmdLine);
  217.     }
  218.     while (ptNext)
  219.         GetMessages(pt); /* We only allow one exec at any one time */
  220.     ptNext = pt;
  221.     ptLast = pt;
  222.     for (ptParent = pt;
  223.          ptParent->hTask == HTASK_FLEDGELING;
  224.          ptParent = _tasks + ptParent->iParent);
  225.     htaskParent = ptParent->hTask;
  226.     _tasks[pt->iParent].iFledgeling = -1;
  227.     pt->iParent = 0;    /* We don't keep this because the parent
  228.                  * may die.
  229.                  */
  230.     pidParent = ptParent->pid;
  231.     pidChild = 0;
  232.     if ((iNew = WinExec(pchCommand, SW_SHOW)) < 32)
  233.     {
  234.         ptParent->nError = aiExecErrors[iNew];
  235.         ptParent->iFledgeling = -1;
  236.         ptNext = 0;
  237.         task_is_dead(pt - _tasks);
  238.         htaskParent = 0;
  239.         pt->nFlags &= ~TF_EXEC;
  240.         pt->nError = ENOENT;
  241.         tkern_wakeup_call();
  242.         return -1;
  243.     }
  244.     free(pchCommand);
  245.     ptParent->nChildren++;
  246.     ptNext = 0;
  247.     htaskParent = 0;
  248.     tkern_wakeup_call();
  249.     pt->nFlags &= ~TF_EXEC;
  250.     if (pt->hTask == HTASK_FLEDGELING)
  251.     {
  252.         ptParent->iFledgeling = -1;
  253.         task_is_dead(pt - _tasks);
  254.     }
  255.     return pidChild;
  256. }
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263. /*
  264.  * The three forms of wait.
  265.  *
  266.  * First, the original wait, which waits unconditionally until the child
  267.  * dies.
  268.  *
  269.  * Second, wait3, which takes flags. It is also supposed to return
  270.  * resource usage information but we don't keep that.
  271.  *
  272.  * Third, waitpid, the POSIX wait. This is to be preferred over wait3.
  273.  */
  274.  
  275. short far _export
  276. tkern_wait(union wait *wstatus)
  277. {
  278.     int    i;
  279.     short    pid;
  280.  
  281.     struct task *pt;
  282.     struct task *ptParent;
  283.  
  284.     pt = GetTaskInfo();
  285.  
  286.     /* Fledgelings cannot be parents */
  287.     for (ptParent = pt;
  288.          ptParent->hTask == HTASK_FLEDGELING;
  289.          ptParent = _tasks + ptParent->iParent);
  290.  
  291.     /* If the parent is childless, return immediately */
  292.     if (!ptParent->nChildren)
  293.         return 0;
  294.  
  295.     /* Wait until the parent has dead children */
  296.     while (!ptParent->nZombies)
  297.         GetMessages(pt);
  298.  
  299.     /* Reap the first dead child we encounter and return its pid */
  300.     for (i = 0; i < nZombies; i++)
  301.     {
  302.         if (_zombies[i].pidParent == ptParent->pid)
  303.         {
  304.             pid = _zombies[i].pid;
  305.             wstatus->w_status = 0;
  306.             wstatus->w_retcode = _zombies[i].nRetCode;
  307.             if (i < nZombies - 1)
  308.                 _zombies[i] = _zombies[nZombies - 1];
  309.             nZombies--;
  310.             ptParent->nZombies--;
  311.             ptParent->nChildren--;
  312.             return pid;
  313.         }
  314.     }
  315.     return 0;
  316. }
  317.  
  318.  
  319.  
  320.  
  321.  
  322. short far _export
  323. tkern_wait3(union wait *wstatus,
  324.         int    nFlags,
  325.         struct rusage *pZero)
  326. {
  327.     int    i;
  328.     short    pid;
  329.     struct task *pt;
  330.     struct task *ptParent;
  331.  
  332.     pt = GetTaskInfo();
  333.  
  334.     if (pZero)
  335.     {
  336.         pt->nError = EINVAL;
  337.         return -1;
  338.     }
  339.  
  340.     /* Fledgelings cannot be parents */
  341.     for (ptParent = pt;
  342.          ptParent->hTask == HTASK_FLEDGELING;
  343.          ptParent = _tasks + ptParent->iParent);
  344.  
  345.     /* If the parent is childless, return immediately */
  346.     if (!ptParent->nChildren)
  347.         return 0;
  348.  
  349.     /* If the WNOHANG flag is supplied, and there are no
  350.      * zombies, return immediately
  351.      */
  352.     if (!ptParent->nZombies && (nFlags & WNOHANG))
  353.         return 0;
  354.  
  355.     /* Since we don't have BSD jobs, we can't use WUNTRACED */
  356.  
  357.     /* Wait until the parent has dead children */
  358.     while (!ptParent->nZombies)
  359.         GetMessages(pt);
  360.  
  361.     /* Reap the first dead child we encounter and return its pid */
  362.     for (i = 0; i < nZombies; i++)
  363.     {
  364.         if (_zombies[i].pidParent == ptParent->pid)
  365.         {
  366.             pid = _zombies[i].pid;
  367.             wstatus->w_status = 0;
  368.             wstatus->w_retcode = _zombies[i].nRetCode;
  369.             if (i < nZombies - 1)
  370.                 _zombies[i] = _zombies[nZombies - 1];
  371.             nZombies--;
  372.             ptParent->nZombies--;
  373.             ptParent->nChildren--;
  374.             return pid;
  375.         }
  376.     }
  377.     return 0;
  378. }
  379.  
  380.  
  381.  
  382.  
  383.  
  384. short far _export
  385. tkern_waitpid(    int    pid,
  386.         union wait *wstatus,
  387.         int    nFlags)
  388. {
  389.     int    i;
  390.     struct task *pt;
  391.     struct task *ptParent;
  392.     BOOL    bZombie;
  393.     int    iEntry;
  394.     struct tk_process *pProc;
  395.     int    nLastZombies = -1;
  396.  
  397.     pt = GetTaskInfo();
  398.  
  399.     /* Fledgelings cannot be parents */
  400.     for (ptParent = pt;
  401.          ptParent->hTask == HTASK_FLEDGELING;
  402.          ptParent = _tasks + ptParent->iParent);
  403.  
  404.     /* If the parent is childless, return immediately */
  405.     if (!ptParent->nChildren)
  406.         return 0;
  407.  
  408.     while (1)
  409.     {
  410.         /* Find the process entry for this child */
  411.         if (pid)
  412.         {
  413.             if (ptParent->nZombies != nLastZombies)
  414.             {
  415.                 iEntry = -1;
  416.                 for (i = 0;  iEntry == -1 && i < nProcesses; i++)
  417.                 {
  418.                     if (_process[i].pid == pid)
  419.                     {
  420.                         iEntry = i;
  421.                         bZombie = FALSE;
  422.                         pProc = &_process[i];
  423.                     }
  424.                 }
  425.                 for (i = 0; iEntry == -1 && i < nZombies; i++)
  426.                 {
  427.                     if (_zombies[i].pid == pid)
  428.                     {
  429.                         iEntry = i;
  430.                         bZombie = TRUE;
  431.                         pProc = &_zombies[i];
  432.                     }
  433.                 }
  434.                 if (pProc->pidParent != ptParent->pid)
  435.                 {
  436.                     pt->nError = EACCES;
  437.                     return -1;
  438.                 }
  439.                 nLastZombies = ptParent->nZombies;
  440.                 if (iEntry == -1)
  441.                 {
  442.                     pt->nError = EFAULT;
  443.                     return -1;
  444.                 }
  445.                 if (!bZombie && (nFlags & WNOHANG))
  446.                     return 0;
  447.                 if (bZombie)
  448.                 {
  449.                     wstatus->w_status = 0;
  450.                     wstatus->w_retcode = _zombies[iEntry].nRetCode;
  451.                     nZombies--;
  452.                     if (iEntry < nZombies)
  453.                         _zombies[i] = _zombies[nZombies];
  454.                     ptParent->nZombies--;
  455.                     ptParent->nChildren--;
  456.                     return pid;
  457.                 }
  458.             }
  459.             else if (!ptParent->nZombies && (nFlags & WNOHANG))
  460.             {
  461.                 return 0;
  462.             }
  463.         }
  464.         else if (ptParent->nZombies)
  465.         {
  466.             for (i = 0; i < nZombies; i++)
  467.             {
  468.                 if (_zombies[i].pidParent == ptParent->pid)
  469.                 {
  470.                     pid = _zombies[i].pid;
  471.                     wstatus->w_status = 0;
  472.                     wstatus->w_retcode = _zombies[i].nRetCode;
  473.                     if (i < nZombies - 1)
  474.                         _zombies[i] = _zombies[nZombies - 1];
  475.                     nZombies--;
  476.                     ptParent->nZombies--;
  477.                     ptParent->nChildren--;
  478.                     return pid;
  479.                 }
  480.             }
  481.         }
  482.         else if (nFlags & WNOHANG)
  483.         {
  484.             return 0;
  485.         }
  486.  
  487.         /* Wait until the parent has dead children */
  488.         GetMessages(pt);
  489.     }
  490. }
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498. /* tkern_fork only handles the internal (to tkern) part of fork().
  499.  * the Throw/Catch functions must be handled in the child program.
  500.  * consequently, fork is represented as a macro.
  501.  */
  502.  
  503. int far _export
  504. tkern_fork(void)
  505. {
  506.     struct    task *pt;
  507.     int    i, j;
  508.  
  509.     pt = GetTaskInfo();
  510.     for (i = 0; i < TNTASK; i++)
  511.     {
  512.         if (!_tasks[i].hTask)
  513.             break;
  514.     }
  515.     if (i == TNTASK)
  516.     {
  517.         pt->nError = ENOMEM;
  518.         return -1;
  519.     }
  520.     pt->iFledgeling = i;
  521.     _tasks[i].hTask = HTASK_FLEDGELING;
  522.     _tasks[i].iParent = (pt - _tasks);
  523.     memcpy(_tasks[i].files, pt->files, sizeof(_tasks[i].files));
  524.     for (j = 0; j < UFILE_MAX; j++)
  525.     {
  526.         if (_tasks[i].files[j] != -1)
  527.             _files[_tasks[i].files[j]].tf_cnt++;
  528.     }
  529.     return 0; /* When we return, we are in the "child" process */
  530. }
  531.  
  532.  
  533. int far _export
  534. tkern_total_zombies(void)
  535. {
  536.     return nZombies;
  537. }
  538.  
  539. int far _export
  540. tkern_list_zombies(    struct tk_process *pList,
  541.             int    nEntries)
  542. {
  543.  
  544.     if (nZombies < nEntries)
  545.         nEntries = nZombies;
  546.     memcpy(pList, _zombies, sizeof(*pList) * nEntries);
  547.     return nEntries;
  548. }
  549.  
  550. /* Note that because tkern_get_process takes an HTASK, it can
  551.  * only return an active process, not a zombied one
  552.  */
  553. int far _export
  554. tkern_get_process(    HTASK    hTask,
  555.             struct    tk_process *tk_proc)
  556. {
  557.     int    i;
  558.  
  559.     for (i = 0; i < nProcesses; i++)
  560.     {
  561.         if (_process[i].hTask == hTask)
  562.         {
  563.             *tk_proc = _process[i];
  564.             return _process[i].pid;
  565.         }
  566.     }
  567.     return 0;
  568. }
  569.  
  570.  
  571.  
  572. int    far    tkern_valid_file(int fd);
  573.  
  574. void far _export
  575. tkern_register_program(    int    *argc,
  576.             char    ***argv,
  577.             char    ***envp)
  578. {
  579.     int    i;
  580.     struct task *pt;
  581.     TASKENTRY te;
  582.  
  583.     TaskFindHandle(&te, GetCurrentTask());
  584.     if (htaskParent)
  585.     {
  586.         /* Because of the way tkern_exec() is written,
  587.          * the only way this can be true is if this
  588.          * is the task spawned from the tkern_exec()
  589.          */
  590.         pt = ptNext;
  591.         pt->hTask = GetCurrentTask();
  592.         for (i = 0; pt->argv[i]; i++);
  593.         *argc = i;
  594.         *argv = pt->argv;
  595.         *envp = pt->envp;
  596.         pt->pid = pidCurrent;
  597.     }
  598.     else
  599.     {
  600.         pt = GetTaskInfo();
  601.         if (pt->argv)
  602.         {
  603.             for (i = 0; *pt->argv; i++);
  604.             *argc = i;
  605.             *argv = pt->argv;
  606.             *envp = pt->envp;
  607.         }
  608.         else
  609.         {
  610.             *argc = 0;
  611.             *argv = 0;
  612.             *envp = 0;
  613.         }
  614.         pt->pid = pidCurrent;
  615.     }
  616.  
  617.     /* We need to flush all messages in the first task because if
  618.      * that task exits without having yielded, TKFMANGR will miss
  619.      * the exit notification.
  620.      */
  621.     if (!nTasks)
  622.         FlushMessages();
  623.     nTasks++;
  624.     while (!hwndManager)
  625.         GetMessages(pt);
  626. }
  627.  
  628. static    void
  629. task_is_dead(    int    iTask)
  630. {
  631.     int    iFile;
  632.  
  633.     if (_tasks[iTask].iFledgeling != -1)
  634.         task_is_dead(_tasks[iTask].iFledgeling);
  635.     for (iFile = 0; iFile < UFILE_MAX; iFile++)
  636.         if (_tasks[iTask].files[iFile] != -1)
  637.             internal_close(iFile, iTask);
  638.     if (_tasks[iTask].argv)
  639.     {
  640.         free(_tasks[iTask].argv);
  641.         _tasks[iTask].argv = 0;
  642.     }
  643.     if (_tasks[iTask].envp)
  644.     {
  645.         free(_tasks[iTask].envp);
  646.         _tasks[iTask].envp = 0;
  647.     }
  648.     _tasks[iTask].hTask = 0;
  649.     _tasks[iTask].intrproc = 0;
  650.     _tasks[iTask].nSignal = 0;
  651.     _tasks[iTask].iFledgeling = -1;
  652. }
  653.  
  654. #pragma argsused
  655. void    far    _export
  656. tkern_program_started(HTASK    htaskNew)
  657. {
  658.     short    iEntry, i;
  659.     short    nPID;
  660.     BOOL    bOK;
  661.  
  662.     if (nProcesses == N_TKPROCS) // Should be impossible
  663.         return;    // Well what else can we do?
  664.     iEntry = nProcesses++;
  665.     do
  666.     {
  667.         nPID = nPIDNext;
  668.         bOK = TRUE;
  669.         for (i = 0; i < nProcesses; i++)
  670.         {
  671.             if (_process[i].pid == nPID)
  672.             {
  673.                 bOK = FALSE;
  674.                 break;
  675.             }
  676.         }
  677.         if (bOK)
  678.         {
  679.             for (i = 0; i < nZombies; i++)
  680.             {
  681.                 if (_zombies[i].pid == nPID)
  682.                 {
  683.                     bOK = FALSE;
  684.                     break;
  685.                 }
  686.             }
  687.         }
  688.         inc_pid(&nPIDNext);
  689.     } while (!bOK);
  690.     _process[iEntry].pid = nPID;
  691.     _process[iEntry].hTask = htaskNew;
  692.     if (ptNext && !pidChild)
  693.     {
  694.         _process[iEntry].pidParent = pidParent;
  695.         _process[iEntry].hTaskParent = htaskParent;
  696.         pidChild = nPID;
  697.         _process[iEntry].iParent = ptParent - _tasks;
  698.     }
  699.     else
  700.     {
  701.         _process[iEntry].pidParent = 1;
  702.         _process[iEntry].hTaskParent = 0;
  703.         _process[iEntry].iParent = -1;
  704.     }
  705.     pidCurrent = nPID;
  706. }
  707.  
  708. void    far    _export
  709. tkern_program_dead(    HTASK    htaskCorpse,
  710.             int    nRetCode)
  711. {
  712.     int    i;
  713.     short    nPID = 0;
  714.  
  715.     /* First, clean up the tkern data on the task, along with
  716.      * all open files.
  717.      */
  718.  
  719.     for (i = 0; i < TNTASK; i++)
  720.     {
  721.         if (_tasks[i].hTask == htaskCorpse)
  722.         {
  723.             task_is_dead(i);
  724.             nTasks--;
  725.             if (!nTasks)
  726.                 SendMessage(hwndManager, TKWM_ALLDONE, 0, 0);
  727.             break;
  728.         }
  729.     }
  730.  
  731.     /* Next, look for any children, and the process itself, in the process
  732.      * list.
  733.      */
  734.     for (i = 0; i < nProcesses; i++)
  735.     {
  736.         /* Change any orphaned processes to pidParent = 1, hTaskParent = 0 */
  737.         if (_process[i].hTaskParent == htaskCorpse)
  738.         {
  739.             /* A process has become orphaned */
  740.             _process[i].hTaskParent = 0;
  741.             _process[i].pidParent = 1;
  742.             _process[i].iParent = -1;
  743.         }
  744.  
  745.         if (_process[i].hTask == htaskCorpse)
  746.         {
  747.             /* This is the process entry for this task */
  748.             nPID = _process[i].pid;
  749.  
  750.             /* If there is room in the zombies table, and the process
  751.              * has a living parent, copy the process entry to the zombies
  752.              * table, and issue a wakeup call for any processes waiting
  753.              * for children.
  754.              *
  755.              * note that if the process has no parents, failing to copy
  756.              * it to the zombie table automatically causes it to be reaped.
  757.              */
  758.             if (nZombies < N_TKPROCS && _process[i].iParent != -1)
  759.             {
  760.                 _zombies[nZombies] = _process[i];
  761.                 _zombies[nZombies].nRetCode = nRetCode;
  762.                 _tasks[_process[i].iParent].nZombies++;
  763.                 nZombies++;
  764.                 tkern_wakeup_call();
  765.             }
  766.  
  767.             nProcesses--;
  768.             if (i != nProcesses)
  769.             {
  770.                 _process[i] = _process[nProcesses];
  771.                 i--;
  772.             }
  773.         }
  774.     }
  775.  
  776.     /* Search for any zombie children of the current process and reap them */
  777.     for (i = 0; i < nZombies; i++)
  778.     {
  779.         if (_zombies[i].pidParent == nPID)
  780.         {
  781.             nZombies--;
  782.             if (i != nZombies)
  783.             {
  784.                 _zombies[i] = _zombies[nZombies];
  785.                 i--;
  786.             }
  787.         }
  788.     }
  789. }
  790.  
  791.  
  792. void
  793. process_init(void)
  794. {
  795.     TASKENTRY te;
  796.     HTASK    htaskNow;
  797.     int    i, j;
  798.  
  799.     for (i = 0; i < TNTASK; i++)
  800.     {
  801.         _tasks[i].hTask = 0;
  802.         _tasks[i].argv = 0;
  803.         _tasks[i].envp = 0;
  804.         _tasks[i].pid = 0;
  805.         _tasks[i].nChildren = 0;
  806.         _tasks[i].nZombies = 0;
  807.         _tasks[i].iFledgeling = -1;
  808.         for (j = 0; j < UFILE_MAX; j++)
  809.             _tasks[i].files[j] = -1;
  810.     }
  811.     htaskNow = GetCurrentTask();
  812.     te.dwSize = sizeof(TASKENTRY);
  813.     TaskFirst(&te);
  814.     memset(_process, 0, sizeof(_process));
  815.     memset(_zombies, 0, sizeof(_zombies));
  816.     do
  817.     {
  818.         if (htaskNow == te.hTask)
  819.             pidCurrent = nPIDNext;
  820.         _process[nProcesses].pid = nPIDNext++;
  821.         _process[nProcesses].pidParent = 1;
  822.         _process[nProcesses].hTaskParent = 0;
  823.         _process[nProcesses].hTask = te.hTask;
  824.         _process[nProcesses].iParent = -1;
  825.         nProcesses++;
  826.     } while (TaskNext(&te));
  827. }
  828.  
  829.  
  830. int far _export
  831. tkern_kill(    int    pid,
  832.         int    nSignal)
  833. {
  834.     int    i;
  835.     struct    task *pt;
  836.  
  837.     pt = GetTaskInfo();
  838.  
  839.     for (i = 0; i < nProcesses; i++)
  840.     {
  841.         if (_process[i].pid == pid)
  842.         {
  843.             switch(nSignal)
  844.             {
  845.             case 0:
  846.                 break;
  847.             case 1:
  848.             case 2:
  849.             case 9:
  850.             case 14:
  851.                 TerminateApp(_process[i].hTask, NO_UAE_BOX);
  852.                 break;
  853.  
  854.             default:
  855.                 TerminateApp(_process[i].hTask, UAE_BOX);
  856.                 break;
  857.             }
  858.             return 0;
  859.         }
  860.     }
  861.     for (i = 0; i < nZombies; i++)
  862.     {
  863.         if (_process[i].pid == pid)
  864.             return 0;    /* Can't kill the undead */
  865.     }
  866.     pt->nError = EFAULT;
  867.     return -1;
  868. }
  869.  
  870.  
  871.  
  872.  
  873. void    far _export
  874. tkern_register_sighandler(void far pascal (*_proc)(int))
  875. {
  876.     struct    task *pt;
  877.  
  878.     pt = GetTaskInfo();
  879.     pt->intrproc = _proc;
  880. }
  881.  
  882. void    far _export
  883. tkern_deliver_signal(    int    nDevice,
  884.             int    nFile,
  885.             int    nSignal)
  886. {
  887.     /* Find any task which is attached to the file and device,
  888.      * and set its nSignal to the value supplied.
  889.      */
  890.  
  891.     int    idFile;
  892.     int    iTask;
  893.     int    iPTF;
  894.  
  895.     for (idFile = 0; idFile < TNFILE; idFile++)
  896.     {
  897.         if (_files[idFile].tf_dev == nDevice &&
  898.             _files[idFile].tf_id == nFile)
  899.         {
  900.             break;
  901.         }
  902.     }
  903.     if (idFile == TNFILE)
  904.         return;        /* Shouldn't be possible */
  905.     for (iTask = 0; iTask < TNTASK; iTask++)
  906.     {
  907.         for (iPTF = 0; iPTF < UFILE_MAX; iPTF++)
  908.         {
  909.             if (_tasks[iTask].files[iPTF] == idFile)
  910.             {
  911.                 if (!_tasks[iTask].intrproc)
  912.                     continue;
  913.                 _tasks[iTask].nSignal = nSignal;
  914.                 if (!(_tasks[iTask].nFlags & TF_ASLEEP))
  915.                     continue;
  916.                 PostAppMessage(_tasks[iTask].hTask, TKWM_WAKEUP, 0, 0);
  917.             }
  918.         }
  919.     }
  920. }
  921.